package dfs_bfs;

import java.util.*;

public class hu_service {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] used = new int[n];
        List<List<Integer>> adj = new ArrayList<>();
        List<Integer> re = new ArrayList<>();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            adj.add(new ArrayList<>());
            String s = sc.nextLine();
            String[] str = s.split(",");
            for (int j = 1; j < str.length; j++) {
                adj.get(i).add(Integer.parseInt(str[j]));
            }
        }
        if(!dfs(m, used, adj, re)){
            System.out.println("-1");
            return;
        }
        if(re.size() == 0){
            System.out.println("null");
            return;
        }
        Collections.sort(re);
        for (int i = 0; i < re.size(); i++) {
            if(i != 0 && re.get(i) == re.get(i - 1))
                continue;
            if(i != re.size() - 1)
                System.out.print(re.get(i) + ",");
            else
                System.out.print(re.get(i));
        }
    }
    public static boolean dfs(int i, int[] used, List<List<Integer>> adj, List<Integer> re){
        if(used[i] == 2)
            return false;
        if(used[i] == 1)
            return true;
        used[i] = 2;
        for (int j = 0; j < adj.get(i).size(); j++) {
            re.add(adj.get(i).get(j));
            if(!dfs(adj.get(i).get(j), used, adj, re))
                return false;
        }
        used[i] = 1;
        return true;
    }
}
/*
4
2
0
1,0
1,1
2,0,1



4
0
0
1,0
1,1
2,0,1



4
2
0
1,0
1,3
2,0,1


4
2
0
1,2
1,1
2,0,1
 */